#include "stdafx.h"
#include "gbuffer.h"
#include "d3d.h"
namespace rendering
{
	namespace gbuffer
	{
		GBuffer create(shared_ptr<d3d::DXDevice> device, Size size)
		{
			uint width = size.w;
			uint height = size.h;

			GBuffer gbuffer;
			gbuffer.albedo = device->create_texture2d(width, height, DXGI_FORMAT_R8G8B8A8_UNORM, 
				D3D11_BIND_RENDER_TARGET | D3D11_BIND_SHADER_RESOURCE);
			gbuffer.normal = device->create_texture2d(width, height, DXGI_FORMAT_R32G32B32A32_FLOAT, 
				D3D11_BIND_RENDER_TARGET | D3D11_BIND_SHADER_RESOURCE);	
			gbuffer.depth = device->create_texture2d(width, height, DXGI_FORMAT_R32_TYPELESS, 
				D3D11_BIND_DEPTH_STENCIL | D3D11_BIND_SHADER_RESOURCE);

			gbuffer.albedo_rtv = device->create_rtv(gbuffer.albedo);
			gbuffer.normal_rtv = device->create_rtv(gbuffer.normal);
			gbuffer.depth_dsv = device->create_dsv(gbuffer.depth);

			gbuffer.albedo_srv = device->create_srv(gbuffer.albedo);
			gbuffer.normal_srv = device->create_srv(gbuffer.normal);
			gbuffer.depth_srv = device->create_srv(gbuffer.depth, DXGI_FORMAT_R32_FLOAT, D3D11_SRV_DIMENSION_TEXTURE2DMS);
			return gbuffer;
		}
		void destroy(GBuffer gbuffer)
		{
			gbuffer.albedo_rtv->Release();
			gbuffer.normal_rtv->Release();
			gbuffer.depth_dsv->Release();

			gbuffer.albedo_srv->Release();
			gbuffer.normal_srv->Release();
			gbuffer.depth_srv->Release();

			gbuffer.albedo->Release();
			gbuffer.normal->Release();
			gbuffer.depth->Release();			
		}
		void resize( shared_ptr<d3d::DXDevice> device, GBuffer* gbuffer, Size size )
		{
			destroy(*gbuffer);
			ZeroMemory(gbuffer, sizeof(GBuffer));
			*gbuffer = create(device, size);
		}

	}
}